package com.scoy.honeymei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.login.LoginActivity;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.utils.SPHelper;

import io.dcloud.adnative.UniAdManager;
import io.dcloud.adnative.model.ISplashADListener;
import io.dcloud.adnative.util.DoToast;
import io.dcloud.adnative.util.Logger;

/**
 * @Description: LmggActivity
 * @Author: qingquan.du
 * @Date: 2020/8/18 14:09
 */
public class LmggActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//
        setContentView(R.layout.activity_gglm);
        final LinearLayout llSplashContainer = findViewById(R.id.ll_splash_container);
        ViewTreeObserver vto = llSplashContainer.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                llSplashContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                loadSplashAD(llSplashContainer.getWidth(),llSplashContainer.getHeight(),llSplashContainer);
            }
        });


    }


    /**
     * 开始splash 广告
     * @param width 宽度
     * @param height 高度
     * @param parent
     */
    private void loadSplashAD(int width, int height, ViewGroup parent){

        UniAdManager.SplashOption splashOption = new UniAdManager.SplashOption();
        splashOption.expectPXWidth = width;
        splashOption.expectPXHeight = height;

        UniAdManager.showSplashAD(splashOption, parent, new ISplashADListener() {

            @Override
            public void onLoadError(int code, String message) {
                gotoMainActivity();
                LmggActivity.this.finish();
            }


            @Override
            public void onLoadSuccess() {
            }

            @Override
            public void onADClick(View view, int type) {
            }

            @Override
            public void onAdShow(View view, int type) {
            }



            @Override
            public void onAdTimeOver() {
                gotoMainActivity();
                LmggActivity.this.finish();
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                Logger.d("onDownloadActive  ---  " + currBytes);
            }


            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                Logger.d("onDownloadFailed  ---  " + currBytes);
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                Logger.d("onDownloadFinished  ---  " + fileName);
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                Logger.d("onInstalled  ---  " + fileName);
            }
        });
    }

    /**
     * 跳转主界面的代码示例
     */
    private void gotoMainActivity(){
        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
            Intent mainIntent = new Intent(LmggActivity.this,MainActivity.class);
            startActivity(mainIntent);
        } else {
            startActivity(new Intent(LmggActivity.this, LoginActivity.class));
        }
    }
}
